<?php
apf_require_controller("QaAbstract");
apf_require_class('Tools_Uploader');
apf_require_class('Const_QaDoc');
class Qa_Finance_DownloadController extends QaAbstractController{

    public function handle_request(){
        $matches = $this->request->get_router_matches();
        $qa_id = intval($matches[1]);
        $upDir = Const_QaDoc::PATH_BASE . Const_QaDoc::PATH_UPLOAD;

        $qa = $this->get_question_bll()->get_question_one($qa_id);
        if($qa['file']){
            //获取文件路径+文件名
            $file = $this->_get_file_dir() . $qa['file'];
            //判断是否存在
            if(file_exists($file)){
                $fo = fopen($file, "r");
                header("Content-type: text/plain; charset=utf-8");
                header("Content-type: application/octet-stream");
                header("Accept-Ranges: bytes");
                header("Accept-Length: " . filesize($file));
                //下载操作
                header("Content-Disposition: attachment; filename=" . rawurlencode($qa['title']) . "." . $this->_get_file_ext($file));
                echo fread($fo, filesize($file));
                fclose($fo);
                exit(0);
            }
        }
        //跳转到错误页面
        $this->show_tips(null, "/qa/");
    }

    /**
     * 获取文件路径
     * Enter description here ...
     */
    private function _get_file_dir(){
        return Const_QaDoc::PATH_UPLOAD;
    }
    /**
     * 获取文件扩展名
     * Enter description here ...
     */
    private function _get_file_ext($file){
        return pathinfo($file, PATHINFO_EXTENSION);
    }

}